<template>
  <view>
    <view class="input-search">
    	<uni-easyinput prefixIcon="search"  trim="all" v-model="keyword" :styles="styles" :placeholderStyle="placeholderStyle"></uni-easyinput>
		<button @click="search">搜索</button>
    </view>
    
    <!-- stripe type="selection" 开启多选 -->
    <uni-table ref="table" type="selection" :loading="loading" emptyText="暂无更多数据" filter-type="search"  @selection-change="selectionChange">
	  <uni-tr>
		<uni-th>
			<button class="close_btn del_btn" @click="toDelete">删除</button>
		</uni-th>
	</uni-tr>
        <uni-tr v-for="(item, index) in card_arr" :key="index" >
			<uni-td width="100%" align="left" class="td-class" >
				<div @longpress="longPressDelete(index)" class="td_div">
					<view class="td_list">
						<text class="label">物料名称：</text>
						<text class="text">{{ item.inv_name }}</text>
					</view>
					<view class="td_list">
						<text class="label">编码：</text>
						<text class="text">{{ item.inv_number }}</text>
					</view>
					<view class="td_list">
						<text class="label">规格：</text>
						<text class="text">{{ item.inv_specification }}</text>
					</view>
					<view class="td_list">
						<text class="label">批号：</text>
						<text class="text">{{ item.cBatch }}</text>
					</view>
					<view class="td_list">
						<text class="label">基本单位</text>
						<text class="text">{{ item.inv_unit }}</text>
					</view>
					<view class="td_list">
						<text class="label">常用单位</text>
						<text class="text">{{ item.store_unit }}</text>
					</view>
					<view class="td_list">
						<text class="label">库存：</text>
						<text class="text">{{ item.kc_qty }}</text>
					</view>
					<view class="td_list">
						<text class="label">原路径：</text>
						<text class="text">{{ item.shelves_name=='undefined'?'':item.shelves_name }}</text>
					</view>
					<view class="td_list">
						<text class="label">现路径：</text>
						<text class="text">{{ item.to_shelves_name }}</text>
					</view>
					<view class="td_list">
						<text class="label">基本数量：</text> 
						<text class="text">{{ item.allot_qty }}</text>
					</view>
					<view class="td_list">
						<text class="label">常用数量：</text> 
						<text class="text">{{ item.allot_auxqty }}</text>
					</view>
				</div>
			</uni-td>
        </uni-tr>
    </uni-table>
	
    <view class="uni-pagination-box">
		<uni-pagination show-icon :page-size="pageSize" :current="currentPage"
        :total="total" @change="change" />
	</view>
	
  </view>
</template>

<script>
// 引入uni-ui组件
import {
  uniPagination,
  uniIcons,
  uniCard,
  uniTable,
  uniTr,
  uniTh,
  uniTd,
  uniEasyinput,
  //uniDataSelect
} from "@dcloudio/uni-ui"

export default {
	components: {
	  uniPagination,
	  uniTable,
	  uniTr,
	  uniTh,
	  uniTd,
	  uniEasyinput,
		//uniDataSelect
	},
  data() {
    return {
      card_arr: [],//当前页码的表格数据
      pageSize: 15,// 每页数据量
      currentPage: 1,// 当前页
      total: 0,// 数据总量
      loading: false,
	  placeholderStyle:'font-size:17px;',
	  styles:{
	  	borderColor: '#999'
	  },
	  keyword:'',
	  where:'',
	  selectedIndexs:[],//选中
      head:{}
    }
  },
  methods: {
    // 多选
	selectionChange(e) {
		this.selectedIndexs = e.detail.index
	},
    toDelete() {
		let _this = this;
		let delFlag = true;
      uni.showModal({
          title: '确定要删除记录？',
          success: function (res) {
              if (res.confirm) {
				  if(_this.selectedIndexs.length>0){
					 _this.selectedIndexs.forEach(key=>{
						 if(_this.head.syn_status==1){
							 _this.$Api.msg("已同步，无法删除");
							 _this.$refs.table.clearSelection()
							 delFlag = false;
						 }
					 })
					 
					 if(delFlag){
						//删除本地记录
						uni.showLoading({});
						_this.selectedIndexs.forEach(key=>{
								_this.$Db.deleteTableData('allot_item','id',_this.card_arr[key].id)
								//更新表头总数
								_this.$Db.exeSQL("update allot set totalnum=(select COALESCE(sum(allot_qty),0) from allot_item where FBillNo='"+_this.card_arr[key].FBillNo+"') where FBillNo='"+_this.card_arr[key].FBillNo+"'")
								_this.$Api.msg('删除成功','success')
								_this.$refs.table.clearSelection()
						})
						uni.hideLoading()
						_this.getlist()
					 }
				  }
              } else if (res.cancel) {
                  return false;
              }
          }
      });
    },
    longPressDelete(index){
    	//长按 删除
    	this.selectedIndexs = []
    	this.selectedIndexs.push(index)
    	this.toDelete()
    },
	// 搜索
	search() {
		this.currentPage = 1;
		this.getlist();
	},
    //获取列表
    async getlist() {
		uni.showLoading();
		let _this = this
		this.where = " FBillNo = '"+this.head.FBillNo+"' "
		if(this.keyword!=''){
			this.where += " and (shelves_name like '%"+this.keyword+"%' or to_shelves_name like '%"+this.keyword+"%' or inv_name like '%"+this.keyword+"%' or inv_number like '%"+this.keyword+"%' or inv_specification like '%"+this.keyword+"%') "
		}
		await this.$Db.queryCount('allot_item',this.where).then(res => {
			this.total = res[0].num;//获得总页数
		})
		
		await this.$Db.queryDataList('allot_item',this.currentPage,this.pageSize,this.where).then(res => {
			this.card_arr = res
		})
		uni.hideLoading();
    },
    change(e) {
      this.currentPage = e.current
      this.getlist()
    }
  },
  mounted(e) {},
  onLoad(e) {
	/* 监听 */
	uni.$once("getAllot", (value) => {
		this.head = value
		this.getlist()
	})
  }
}
</script>